package com.woniuxy.stage3demo.controller.gy;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.gy.OverdueBean;
import com.woniuxy.stage3demo.bean.gy.OverdueIm;
import com.woniuxy.stage3demo.bean.gy.ResultMesDemo;
import com.woniuxy.stage3demo.bean.gy.UserDemo;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.gy.OverdueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**逾期列表
 * @author ：苟杨
 * @description：TODO
 * @date ：2020/12/14 18:21
 */
@RequestMapping("overdue")
@RestController
public class OverdueController {
    @Resource
    private OverdueService overdueService;
    //查询所有的逾期信息
    @GetMapping(value ="/findAll",produces = {"application/json;charset=utf-8"})
    public TableResult findAll(PageRequest pageRequest, UserDemo userDemo){
        System.out.println(pageRequest);
        System.out.println(userDemo);
        PageInfo<OverdueBean> all = overdueService.findAll(pageRequest.getPageNum(), pageRequest.getLimit(), userDemo);
        long total = all.getTotal();
        List<OverdueBean> ll = all.getList();
        System.out.println(ll);
        TableResult tr = new TableResult<>();
        tr.setRows(ll);
        tr.setTotal(total);

        return tr;
    }


    //查询所有的逾期信息
    @GetMapping(value ="/findAllReduce",produces = {"application/json;charset=utf-8"})
    public TableResult findAllReduce(PageRequest pageRequest, UserDemo userDemo){
        System.out.println(pageRequest);
        System.out.println(userDemo);
        PageInfo<OverdueBean> all = overdueService.findAllReduce(pageRequest.getPageNum(), pageRequest.getLimit(), userDemo);
        long total = all.getTotal();
        List<OverdueBean> ll = all.getList();
        System.out.println(ll);
        TableResult tr = new TableResult<>();
        tr.setRows(ll);
        tr.setTotal(total);

        return tr;
    }


    //查询逾期总体数(逾期人数和金额)
    @GetMapping(value = "/count",produces = {"application/json;charset=utf-8"})
    public OverdueIm Count(){
        //查询每个月的逾期人数
        int countMonth = overdueService.findCountMonth();
        System.out.println(countMonth);
        //查询每年的逾期人数
        int countYear = overdueService.findCountYear();
        System.out.println(countYear);
        //查询每个月的逾期金额
        List<ResultMesDemo> monthMoney = overdueService.findMonthMoney();
        long[] SumMonthMoney = {0};
        monthMoney.forEach(e->{
            long realPrincipal = e.getRealPrincipal();
            SumMonthMoney[0] = SumMonthMoney[0] +realPrincipal;
            long realInterest = e.getRealInterest();
            SumMonthMoney[0] = SumMonthMoney[0] +realInterest;
        });
        System.out.println(SumMonthMoney[0]);
        //查询每年的逾期金额
        List<ResultMesDemo> yearMoney = overdueService.findYearMoney();
         long[] SumYearMoney = {0};
        yearMoney.forEach(e->{
            long realPrincipal = e.getRealPrincipal();
            SumYearMoney[0]=SumYearMoney[0]+realPrincipal;
            long realInterest = e.getRealInterest();
            SumYearMoney[0]=SumYearMoney[0]+realInterest;
        });
        OverdueIm oi = new OverdueIm();
        oi.setCountMonth(countMonth);
        oi.setCountYear(countYear);
        oi.setCountMonthCount(SumMonthMoney[0]);
        oi.setCountYearCount(SumYearMoney[0]);
        System.out.println(oi);
        return oi;
    }

    @GetMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public ResultMsg reduce(int lateCharge,int id){
        ResultMsg rm = new ResultMsg();
        if(lateCharge==0){
            rm.setCode(202);
            return rm;
        }else {
            int i = overdueService.updateLateCharge(id, lateCharge);

            if(i!=0){
                rm.setCode(200);
            }else {
                rm.setCode(201);
            }
            return rm;
        }
    }
}
